﻿#region usings
using System;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Command;
using InsuranceAdjuster.OccasionallyConnected;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Views;
using GalaSoft.MvvmLight.Messaging;
#endregion
namespace InsuranceAdjuster.ViewModels {
    public class SyncSettingsViewModel : ViewModelBase {
        public RelayCommand ChangeNetworkStatusCommand { get; set; }
        public RelayCommand SyncNowCommand { get; set; }
        public RelayCommand ShowAnchorsDialogCommand { get; set; }
        public RelayCommand PurgeCacheCommand { get; set; }

        public SyncSettingsViewModel(IPageConductor pageConductor) {
            this.PageConductor = PageConductor;
            RegisterCommands();
            EntityManager.NetworkAvailabilityChanged += (o, e) => SyncNowCommand.RaiseCanExecuteChanged();
        }

        public Double ActivitySyncInterval {
            get { return EntityManager.CacheMonitor.ActivitySyncIntervalInSeconds; }
            set {
                if (EntityManager.CacheMonitor.ActivitySyncIntervalInSeconds == value)
                    return;

                EntityManager.CacheMonitor.ActivitySyncIntervalInSeconds = value;
                RaisePropertyChanged("ActivitySyncInterval");
            }
        }
        public Double ReferenceSyncInterval {
            get { return EntityManager.CacheMonitor.ReferenceSyncIntervalInSeconds; }
            set {
                if (EntityManager.CacheMonitor.ReferenceSyncIntervalInSeconds == value)
                    return;

                EntityManager.CacheMonitor.ReferenceSyncIntervalInSeconds = value;
                RaisePropertyChanged("ReferenceSyncInterval");
            }
        }

        protected override void RegisterCommands() {
            ChangeNetworkStatusCommand = new RelayCommand(() => EntityManager.ToggleOnOfflineMode());
            SyncNowCommand = new RelayCommand(() => EntityManager.CacheMonitor.SyncNow(), () => EntityManager.IsOnline);
            ShowAnchorsDialogCommand = new RelayCommand(() =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {

                    var MyChild = new FloatableWindow();
                    var MyPanel = new Grid();

                    ((App.Current.RootVisual as InsuranceAdjuster.Controls.BusyIndicator).Content as Grid).Children.Add(MyPanel);
                    MyChild.ParentLayoutRoot = MyPanel;
                    MyChild.Title = "Client Anchors";
                    MyChild.Content = new ClientAnchorsView();
                    MyChild.MinWidth = 400;
                    MyChild.Width = 640;
                    MyChild.MinHeight = 350;
                    MyChild.Height = 520;
                    MyChild.Show();
                });
            });
            PurgeCacheCommand = new RelayCommand(() =>
            {
                EntityManager.GetObjectStore().Clear();
                var MyDialogMessage = new DialogMessage("Cache purged", result => {});
                Messenger.Default.Send<DialogMessage>(MyDialogMessage);
            });
        }
    }
}
